package com.flc.gateway.session;

import com.flc.gateway.server.MyChannelFutureListener;
import io.netty.channel.Channel;

/**
 * @author haizi
 * @date 2018/1/1
 */
public class Session {

    public Session(Channel channel) {
        this.channel = channel;
    }

    /**
     * 实际会话对象
     */
    private Channel channel;

    /**
     * 向会话中写入消息
     * 写入成功与否由监听器异步监听
     * @param obj 要发送的消息 (之后由编码器自动编码)
     */
    public void write(Object obj){
        channel.writeAndFlush(obj).addListener(new MyChannelFutureListener());
    }

    /**
     * 判断会话是否在连接中
     */
    public boolean isConnected() {
        return channel.isActive();
    }

    /**
     * 关闭
     */
    public void close() {
        if (this.channel == null) {
            return;
        }
        if (channel.isOpen()) {
            channel.close();
        }
    }
}
